package com.my.netty.promote.server.handler;

import com.my.netty.promote.message.GroupJoinRequestMessage;
import com.my.netty.promote.message.GroupJoinResponseMessage;
import com.my.netty.promote.server.session.GroupSession;
import com.my.netty.promote.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class GroupJoinRequestMessageHandler extends SimpleChannelInboundHandler<GroupJoinRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupJoinRequestMessage message) throws Exception {
        String groupName = message.getGroupName();
        String username = message.getUsername();
        //暂不做存在校验
        GroupSession groupSession = GroupSessionFactory.getSession();
        groupSession.joinMember(groupName, username);
        GroupJoinResponseMessage responseMessage = new GroupJoinResponseMessage(true, "已加入" + groupName);
        ctx.writeAndFlush(responseMessage);
    }
}
